package message

import (
	"encoding/json"
	"gomonitor-server/src/db"
	"gomonitor-server/src/dto"
	"gomonitor-server/src/mylog"
	"gomonitor-server/src/tcp/connectioncount"
	"gomonitor-server/src/tcp/types"
	"net"
)

func MonitorInfoMessage(messageHandler *MessageHandler, data []byte, conn net.Conn) {
	// 存储库
	var sysInfo dto.SysInfo
	err := json.Unmarshal(data, &sysInfo)
	if err != nil {
		mylog.Logger.Error("转换JSON错误..")
		messageHandler.SendMessage(types.MSG_FROM_SERVER, []byte(err.Error()), types.MSG_TYPE_INFO, conn)
		return
	}
	_, err = json.Marshal(sysInfo)
	if err != nil {
		messageHandler.SendMessage(types.MSG_FROM_SERVER, []byte(err.Error()), types.MSG_TYPE_INFO, conn)
		return
	}
	messageHandler.SendMessage(types.MSG_FROM_SERVER, []byte("ok"), types.MSG_TYPE_INFO, conn)
	mylog.Logger.Debugf("开始记录连接%s的监控信息到数据库", conn.RemoteAddr().String())
	db.RecordDb(sysInfo)
	mylog.Logger.Debugf("RecordDb:%s的监控信息到数据库", conn.RemoteAddr().String())
	connectioncount.UpdateSysInfo(conn, sysInfo)
	mylog.Logger.Debugf("结束记录连接%s的监控信息到数据库", conn.RemoteAddr().String())
}
